/**
 *
 */

$(document).ready(function() {
    var mediciEmails = new Array();
    var materiale;

    console.log("get lista medici");
    $.ajax({
        url : '/ListaMedici',
        type : "GET",
        success : function(data, textStatus, jqXHR) {
            var json = eval("{" + data + "}");
            //data: return data from server
            var html = '<select id="medici"><option value="default">Alege un medic</option>';
            var html2 = '<select id="medici-adauga-material"><option value="default">Alege un medic</option>';
            $.each(json, function(i, v) {
                console.log("json idex : " + i);
                html += '<option value="' + i + '">' + v.numeMedic + '</option>';
                html2 += '<option value="' + i + '">' + v.numeMedic + '</option>';
                mediciEmails[i] = v.email;
            });
            html += '</select>';
            html2 += '</select>';
            console.log("html is: " + html);
            console.log("emails: " + mediciEmails);
            $('#list-medici').append(html);
            $('#new-item-list-medici').append(html2);

        },
        error : function(jqXHR, textStatus, errorThrown) {
            //if fails
        }
    });

    $("#list-medici").on('change', '#medici', function() {
        console.log('option selected');
        var medicEmail = mediciEmails[$(this).find(":selected").val()];
        console.log("Medic selected has email :" + medicEmail);
        $.ajax({
            url : '/ListaMaterialeMedic',
            data : {
                'emaildoctor' : medicEmail
            },
            type : "POST",
            success : function(data, textStatus, jqXHR) {
                materiale = eval("{" + data + "}");
                //data: return data from server
                var html = '<h4>Materiale</h4><select id="materiale"><option value="default">Alege un material</option>';

                $.each(materiale, function(i, v) {
                    console.log("json idex : " + i);
                    html += '<option value="' + i + '">' + v.denumire + '</option>';
                });
                html += '</select>';
                console.log("html is: " + html);
                $('#list-item').html('').append(html);

            },
            error : function(jqXHR, textStatus, errorThrown) {
                //if fails
            }
        });
    });

    $("#list-item").on('change', '#materiale', function() {
        var indexMaterial = $(this).find(":selected").val();
        var json = materiale[indexMaterial];
        var html = '<h4>Info Material</h4><br>Denumire material: ' + json.denumire + '<br>';
        html += 'Cantitate material: ' + json.cantitate + '<br>';
        html += 'Medic: ' + json.emaildoctor + '<br>';
        $('#info-item').html('').append(html);

    });

    $('#adauga-material').on('click', function() {
        var medicEmail = mediciEmails[$('#medici-adauga-material').find(":selected").val()];
        var denumire = $('#denumire-material').val();
        var cantitate = $('#cantitate').val();
        $.ajax({
            url : '/AdaugaMaterial',
            data : {
                'emaildoctor' : medicEmail,
                'denumire' : denumire,
                'cantitate' : cantitate
            },
            type : "POST",
            success : function(data, textStatus, jqXHR) {
                console.log("received data:" + data);
                switch(data) {
                    case 'succes':
                        alert('Material adaugat cu succes');
                        $('input[type="text"]').val('');
                        break;
                    default:
                        alert('Adaugare material esuata');
                        break;
                }

            },
            error : function(jqXHR, textStatus, errorThrown) {
                //if fails
            }
        });
    });

});

